﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gedea.Core.Repositories;
using Gedea.Model.Entities;

namespace Gedea.Service.Services
{
    public class AreaService
    {
        private static List<Area> areas = null;

        public AreaService()
        { //TODO quitar el refresh y reemplazarlo por algo mas performante
            Refresh();
        }

        public void Add(Area area)
        {
            area.UserId = Credentials.currentUser.UserId;
            new AreaRepository().Add(area);

            Refresh();
        }

        public void Update(Area area)
        {
            area.UserId = Credentials.currentUser.UserId;
            new AreaRepository().Update(area);

            Refresh();
        }

        public Area Get(int id)
        {
            Area area = areas.Where(x => x.AreaId == id).FirstOrDefault();
            return area ?? new AreaRepository().Get(id);
        }

        public List<Area> List()
        {
            return areas.Where(x => x.UserId == Credentials.currentUser.UserId).ToList();
        }

        private void Refresh()
        {
            areas = new AreaRepository().List(Credentials.currentUser.UserId);
        }
    }
}
